#include "userMain.h"
#include "userUartTxCtrl.h"

extern uint8_t userTxBuf[];
extern uint8_t TxEndFlag;
void userSendTimeFunc(void)
{
    uint8_t user_i,userCheckSum = 0;
    userTxBuf[0] = 0x31;
    userTxBuf[1] = 0x00;
    userTxBuf[2] = 12;
    userTxBuf[3] = 0x01;
    for(user_i=0;user_i<11;user_i++)
    {
        userCheckSum += userTxBuf[user_i];
    }
    userTxBuf[11] = userCheckSum;
    while(TxEndFlag==0)_NOP;
    UserUartStartDmaTx(12);
}

extern uint8_t userIRdecode[];
extern uint16_t userIRdecodeLen;
extern uint8_t userIrDstAddrH,userIrDstAddrL;
void userSendIrLearnCodeFunc(void)
{
    uint16_t user_i;
    uint8_t userCheckSum = 0;
    userTxBuf[0] = 0x31;
    userTxBuf[1] = (userIRdecodeLen+10) >> 8;
    userTxBuf[2] = (userIRdecodeLen+10) & 0x00FF;
    userTxBuf[3] = 0x02;
    userTxBuf[4] = userIrDstAddrH;
    userTxBuf[5] = userIrDstAddrL;
    userTxBuf[6] = 0x00;
    userTxBuf[7] = 0x01;
    userTxBuf[8] = 0x26;
    for(user_i=0;user_i<userIRdecodeLen;user_i++)
    {
        userTxBuf[user_i+9] = userIRdecode[user_i];
    }
    for(user_i=0;user_i<(userIRdecodeLen+9);user_i++)
    {
        userCheckSum += userTxBuf[user_i];
    }
    userTxBuf[userIRdecodeLen+9] = userCheckSum;
    while(TxEndFlag==0)_NOP;
    UserUartStartDmaTx(userIRdecodeLen+10);  
}

void userSendIrLearnTimeoutFunc(void)                   //IR learn Timeout
{
    uint16_t user_i;
    uint8_t userCheckSum = 0;
    userTxBuf[0] = 0x31;
    userTxBuf[1] = 0x00;
    userTxBuf[2] = 0x07;
    userTxBuf[3] = 0x02;
    userTxBuf[4] = userIrDstAddrH;
    userTxBuf[5] = 0xFF;
    for(user_i=0;user_i<6;user_i++)
    {
        userCheckSum += userTxBuf[user_i];
    }
    userTxBuf[6] = userCheckSum;
    while(TxEndFlag==0)_NOP;
    UserUartStartDmaTx(7);  
}





extern uint8_t userKeyValue;
void userSendKeyInfoFunc(void)          // send keypad info to MASTER
{
    uint8_t user_i,userCheckSum = 0;
    userTxBuf[0] = 0x31;
    userTxBuf[1] = 0x00;
    userTxBuf[2] = 6;
    userTxBuf[3] = 0x05;
    userTxBuf[4] = userKeyValue;
    for(user_i=0;user_i<5;user_i++)
    {
        userCheckSum += userTxBuf[user_i];
    }
    userTxBuf[5] = userCheckSum;
    while(TxEndFlag==0)_NOP;
    UserUartStartDmaTx(6);
}

extern uint8_t userHumidityNow,userTemperatureNow;
void userSendHTinfoFunc(void)
{
    uint8_t user_i,userCheckSum = 0;
    userTxBuf[0] = 0x31;
    userTxBuf[1] = 0x00;
    userTxBuf[2] = 7;
    userTxBuf[3] = 0x03;
    userTxBuf[4] = userTemperatureNow;
    userTxBuf[5] = userHumidityNow;
    for(user_i=0;user_i<6;user_i++)
    {
        userCheckSum += userTxBuf[user_i];
    }
    userTxBuf[6] = userCheckSum;
    while(TxEndFlag==0)_NOP;
    UserUartStartDmaTx(7);
}

extern uint16_t uModOffset;
uint8_t uBmaCnt=0;
uint8_t uBmaFlag = 0;
//uint8_t uAccCntA,uAccCntB;
void userSendAccInfoFunc(void)
{
    uint8_t user_i,userCheckSum = 0;
    //uint16_t user_m;
    userTxBuf[0] = 0x31;
    userTxBuf[1] = 0x00;
    userTxBuf[2] = 07;
    userTxBuf[3] = 0x04;
    userTxBuf[4] = uModOffset >> 8;
    userTxBuf[5] = uModOffset & 0x00FF;
    if(uModOffset < 5)
    {
      uBmaCnt++;
      if(uBmaCnt>1)
      {
        uBmaFlag = 0;
      }
    }
    else
    {
      uBmaFlag = 1;
      uBmaCnt = 0;
    }
    for(user_i=0;user_i<6;user_i++)
    {
        userCheckSum += userTxBuf[user_i];
    }
    userTxBuf[6] = userCheckSum;
    if(uBmaFlag==0) return ;
    while(TxEndFlag==0)_NOP;
    UserUartStartDmaTx(7);
}










